<template>
  <div o-Container>
    <template v-if="isMDM">
      <o-item v-bind:source="item.project" v-bind="{loaded:load,saved:save}"></o-item>
    </template>
    <template v-else>
      <o-list v-bind:source="list.project" v-bind="{created:create,modified:modify}"/>
      <o-dialog v-bind:source="panel" v-bind="{opened:load,closed:load}">
        <template slot="content">
          <o-item v-bind:source="item.project" v-bind="{saved:save,backed:back}"></o-item>
        </template>
      </o-dialog>
    </template>
  </div>
</template>

<script>
  export default {
    name: "mms_cus_project_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
    data() {
      return {
        octopus: this.$parent.octopus,
        isMDM: false,
        list: {
          project: {
            data: {
              id: 63, // 菜单表的八爪鱼ID
              cellData: null,
            }
          },
        },
        item: {
          project: {
            // 以下参数默认由组件内部创建，但是如果有需要，开发人员可在这里提前设置，组件将按照您的预期渲染。
            data: {
              id: 63,
              cellData: [
                {name: "CompanyID", placeholder: "请选择公司"},
                {name: "Abbreviation"},
                {name: "Name", placeholder: "请输入项目名称"},//名称
                {name: "ProvinceID", link: [{mapping: "FatherID", value: 0}]},
                {name: "CityID", link: [{binding: "ProvinceID", mapping: "FatherID"}]},
                {name: "Address"},//地址
                {name: "Code"},////项目代号
                {name: "UniqueCode"},////项目代号
                {name: "Area"},////项目面积
                {name: "CreateTime"},//创建时间
                {name: "Longitude"},
                {name: "Latitude"},
                {name: "HKAppKey"},//海康 appkey
                {name: "HKSecret"},//海康 token
                {name: "HKAccessToken"},//海康 token
                {name: "Introduction", placeholder: "请输入描述"},//介绍
                {name: "ChargeID"},//运营者id
                {name: "EngineerChargeID"},//工程师id
                {name: "EnvironmentChargeID",},//环境负责人
                {name: "SecurityChargeID"},//安全负责人
                {name: "OrderCCUserList"},//工单抄送人员列表
                {name: "PhotoPath"},//图片列表
              ],
            },
          },
        },
        panel: {
          visible: false,
        },
      }
    },
    created: function () {
      this.init();
    },
    methods: {
      init: function () {
        let url = this.octopus.vue.$route.path;
        // MMS 用建筑，需要增加过滤条件
        if (url.indexOf("MMS/Project/Project") > 0) {
          this.list.project.data.cellData = [
            {name: "CompanyID", width: 400, searchKey: "=", placeholder: "请选择公司"},
            {name: "ProvinceID", width: 100, searchKey: "=", link: [{mapping: "FatherID", value: 0}]},
            {name: "CityID", width: 100, searchKey: "=", link: [{binding: "ProvinceID", mapping: "FatherID"}]},
            {name: "Name", width: 150, searchKey: "LIKE"},
            {name: "Abbreviation", width: 100, searchKey: "LIKE"},
            {name: "Code", width: 200, searchKey: "LIKE"},
            {name: "UniqueCode", width: 270, searchKey: "LIKE"},
            {name: "Address", width: 200, searchKey: "LIKE"},
            {name: "Area", width: 100},
            {name: "ChargeID", link: [{mapping: "CompanyID", value: this.octopus.passport.authorization.company}]},
            {
              name: "EngineerChargeID",
              link: [{mapping: "CompanyID", value: this.octopus.passport.authorization.company}]
            },
            {
              name: "EnvironmentChargeID",
              link: [{mapping: "CompanyID", value: this.octopus.passport.authorization.company}]
            },
            {
              name: "SecurityChargeID",
              link: [{mapping: "CompanyID", value: this.octopus.passport.authorization.company}]
            },
            {name: "Introduction", width: 300},
            {name: "CreateTime", width: 160},
          ];
        }
        // MDM
        else {
          this.item.project.data.value = this.octopus.passport.project;
          this.octopus.tools.parseCell(this.item.project.data, "ChargeID").link = [{
            mapping: "CompanyID",//根据公司id来过滤
            value: this.octopus.passport.authorization.company
          }];
          this.octopus.tools.parseCell(this.item.project.data, "EngineerChargeID").link = [{
            mapping: "CompanyID",//根据公司id来过滤
            value: this.octopus.passport.authorization.company
          }];
          this.octopus.tools.parseCell(this.item.project.data, "EnvironmentChargeID").link = [{
            mapping: "CompanyID",//根据公司id来过滤
            value: this.octopus.passport.authorization.company
          }];
          this.octopus.tools.parseCell(this.item.project.data, "SecurityChargeID").link = [{
            mapping: "CompanyID",//根据公司id来过滤
            value: this.octopus.passport.authorization.company
          }];
          this.octopus.tools.parseCell(this.item.project.data, "OrderCCUserList").link = [{
            mapping: "CompanyID",//根据公司id来过滤
            value: this.octopus.passport.authorization.company
          }];

          // 优化支持了初始化可设置。
          this.octopus.tools.parseCell(this.item.project.data, "CompanyID").disabled = true;

          // 已经隐藏了，所以没必要过滤了。
          // this.octopus.tools.parseCell(this.list.project.data, "ID").filterValue = this.octopus.passport.project;//默认筛选当前登录的小区
          this.isMDM = true;
        }
      },
      load: function () {
        // this.octopus.tools.parseCell(this.item.project.data, "CompanyID").disabled = true;
      },
      create: function () {//新增
        this.panel.visible = true;
        this.item.project.data.value = null;
        this.item.project.control.load();
      },
      modify: function (row) {//修改
        this.panel.visible = true;
        this.item.project.data.value = row.ID;
        this.item.project.control.load();
      },
      save: function (data) {//保存
        this.panel.visible = false;
        // let params = {
        //   "type": "Project",
        //   "id": this.item.project.data.value
        // };
        // this.octopus.request.api.post("/ESB/Tools/Transfer/WashRefresh", params);

        if (this.list.project.control) {
          this.list.project.control.load();
        }
        if (data.status.code == 200) {
          this.octopus.tips.success("[" + data.status.code + "] Data Save Success.");
        }
      },
      back: function () {
        this.panel.visible = false; // 取消
      }
    },
    props: ["oData"]
  }
</script>

